#include "std.hpp"
#include "Ynbw.hpp"

int Ynbw::runGame()
{
  try
  {
FUNCTION

    char *input = new char[1024];
    std::string line = "";
    bool showWinner = false;
    int returnValue = 0;

    while(!showWinner)
    {
      for(unsigned int i = 0; i < numberPlayers_; i++)
      {
        if((gamePlayers_[i])->getIsOut()) continue;
        displayPrompt(gamePlayers_[i]->getName());
//        std::cout << (gamePlayers_[i])->getName() << prompt_;
        std::cin.getline(input, 1024, '\n');
        line = input;
        std::transform(line.begin(), line.end(), line.begin(), ::tolower);

        if(line == "quit" || line == "exit" || line == "bye")
        {
          char quitChoice;
          std::cout << "Are You Sure? ";
          std::cin >> quitChoice;
          if(quitChoice == 'y' || quitChoice == 'Y')
          {
            throw EXCEPTION_SUCCESS;
          }
          if(std::cin.peek() == '\n') std::cin.ignore(1, '\n');
        }

        if(dictionary_->proccessLine(line))
        {
          std::cout << "Sorry, " << (gamePlayers_[i])->getName() << "! You're Out!\n";
          playersStillIn_--;
          (gamePlayers_[i])->setIsOut(true);
          if(playersStillIn_ < 2)
          {
            showWinner = true;
            break;
          }
        }
      }
    }

    std::cout << "\nAnd the winner is...\n";
    std::cout << ((whoIsWinner())->getName()) << "!!! Congratulations!\n";
    return returnValue;
  }
  catch(...)
  {
    throw;
  }
}
